package com.julang.check.utils;

import com.google.gson.Gson;
import com.julang.check.bean.TranslateResult;
import com.julang.check.translate.common.LANG;

import java.io.IOException;
import java.util.Date;

import static com.julang.check.utils.CommonData.ERRORCODESTART;
import static com.julang.check.utils.CommonData.HTMLSTART;
import static com.julang.check.utils.MessageInfo.GOOLETRANSLATEERRORINFO;
/**
 * @author 赵自强
 * */
public class LanguageUtil {
    public static String toString(String args) {
        return "";
    }

    public String toString(String[] args) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < args.length; i++) {
            builder.append(args[i]);
            builder.append("\r\n");
        }
        return builder.toString();
    }

    public static int getJifenOfLanguage(String s) {
        return ((s.length() % 1000 == 0) ? s.length() : (s.length() + 1000))/1000;
    };
    public static Result<TranslateResult.TransResult[]> getBaiduRepeatReductionResult(String query) throws IOException {
        Result<TranslateResult.TransResult[]> result = new Result<>();
        String s = null;
        Gson gson = new Gson();
        for (int i = 0; i < SingnalData.TranslateMode.size(); i++) {
            if (i == 0) {
                s = SingnalData.baiduTranslator.getTransResultByPost(query, "zh", SingnalData.TranslateMode.get(i).getId());
            } else {
                s = SingnalData.baiduTranslator.getTransResultByPost(s, SingnalData.TranslateMode.get(i - 1).getId(), SingnalData.TranslateMode.get(i).getId());
            }
            if (s.startsWith(ERRORCODESTART)) {
                result.setResult(false);
                result.setData(null);
                result.setMessage(s);
                return result;
            } else {
                System.out.println(s);
                TranslateResult translateResult = gson.fromJson(s, TranslateResult.class);
                System.out.println(translateResult.toString());
                System.out.println();
                s = translateResult.getTransResult()[0].getDst();
            }
        }
        s = SingnalData.baiduTranslator.getTransResultByPost(s, SingnalData.TranslateMode.get(SingnalData.TranslateMode.size() - 1).getId(), "zh");
        if (s.startsWith(ERRORCODESTART)) {
            result.setResult(false);
            result.setData(null);
            return result;
        }
        TranslateResult translateResult = gson.fromJson(s, TranslateResult.class);
        result.setResult(true);
        result.setData(translateResult.getTransResult());
        System.out.println(SingnalData.df.format(new Date()) + " 结果:" + translateResult.getTransResult()[0].getDst());
        return result;
    }

    public static Result<TranslateResult.TransResult[]> getGooleReductionResult(String query) throws IOException {
        try {
            String ying = SingnalData.googleTranslator.trans(LANG.ZH, LANG.EN, query);
            String z = SingnalData.googleTranslator.trans(LANG.EN, LANG.ZH, ying);
            System.out.println(SingnalData.df.format(new Date()) + " 结果:" + z);
            if (z.startsWith(HTMLSTART)){
                return Result.buildError(CommonData.FAIL,GOOLETRANSLATEERRORINFO,null);
            }else {
                TranslateResult.TransResult[] results = new TranslateResult.TransResult[1];
                results[0] = new TranslateResult.TransResult("", z);
                return Result.buildSuccess(results);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError(CommonData.FAIL,"检测发生异常",null);
        }
    }
    public static Result getJinShanReductionResult(String query) throws IOException {
        Result result = new Result();
        try {
            String ying = SingnalData.jinshanTranslator.trans(LANG.ZH, LANG.EN, query);
            System.out.println(ying);
            String z = SingnalData.jinshanTranslator.trans(LANG.EN, LANG.ZH, ying);
            System.out.println(SingnalData.df.format(new Date()) + " 结果:" + z);
            result.setResult(true);
            result.setData(z);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setResult(false);
            return result;
        }
    }
}
